//
//  WAQiniuMananger.m
//  WalkiOS
//
//  Created by luqiuan on 2019/11/9.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAQiniuMananger.h"

static NSString *qiuniuUrl = @"http://baby921.top/";

@interface WAQiniuMananger ()

@end

@implementation WAQiniuMananger

+ (void)uploadImage:(UIImage *)image key:(NSString *)key progressHandler:(QNUpProgressHandler)progress complete:(QNUpCompletionHandler)completionHandler{
    
    [WNBusinessRequest getQiuneTokenSucc:^(id  _Nonnull responseObject) {
        NSString *token = (NSString *)responseObject;
        [self uploadImageSubFunc:image
                           token:token
                             key:key
                 progressHandler:progress
                        complete:completionHandler];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        completionHandler(nil,mes,nil);
    }];
    
    
}

+ (void)uploadImageSubFunc:(UIImage *)image
                     token:(NSString *)token
                       key:(NSString *)key
           progressHandler:(QNUpProgressHandler)progress complete:(QNUpCompletionHandler)completionHandler{
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil
                                                        progressHandler:progress
                                                                 params:nil
                                                               checkCrc:NO
                                                     cancellationSignal:nil];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.6);
    [upManager putData:imageData
                   key:key
                 token:token
              complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        key = [NSString stringWithFormat:@"%@%@",qiuniuUrl,key];
        completionHandler(info,key,resp);
    }
                option:uploadOption];
}




@end
